package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Bean.BuyingMembersBean;
import com.example.administrator.x2picturetransliteration.Home.Adapter.BuyingMembersAdapter;
import com.example.administrator.x2picturetransliteration.Home.Dialog.PaymentDialog;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Activity.BuyingMembersPresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.Payment.AlipayUtil;
import com.example.administrator.x2picturetransliteration.Util.Payment.WeChatUtil;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;
import com.example.administrator.x2picturetransliteration.wxapi.WXPayEntryActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 购买会员
 * Created by Administrator on 2019/8/27.
 */

public class BuyingMembersActivity extends BaseActivity{


    @BindView(R.id.TitleText)
    TextView TitleText;
    @BindView(R.id.LButton)
    ImageView LButton;

    @BindView(R.id.listview)ListView listview;

    private BuyingMembersPresenter mBuyingMembersPresenter;

    private List<BuyingMembersBean> mData;

    private BuyingMembersAdapter mBuyingMembersAdapter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_buying_members;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText("升级为VIP");
        LButton.setImageResource(R.mipmap.home_backw);

        mBuyingMembersPresenter = new BuyingMembersPresenter(this,mCompositeSubscriptions);

        mData = new ArrayList<>();

        mBuyingMembersAdapter = new BuyingMembersAdapter(this,mData, new BuyingMembersAdapter.hd() {
            @Override
            public void hdff(final String type) {

                new PaymentDialog(BuyingMembersActivity.this, new PaymentDialog.hd() {
                    @Override
                    public void hdff(final boolean judge) {

                        Map<String,String> map = new HashMap<>();
                        map.put("type",type);
                        map.put("payType",judge?"2":"1");

                        mBuyingMembersPresenter.add(map, new BasePresenter.Callback() {
                            @Override
                            public void getData(Object data) {

                                if (judge){

                                    String aaa = data + "";

                                    zfb(data + "");

                                }else {

                                    wx((Map<String,String>)data);
                                }
                            }
                        });

                    }
                }).show();
            }
        });

        listview.setAdapter(mBuyingMembersAdapter);

        mBuyingMembersPresenter.list(new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                mData.addAll((List<BuyingMembersBean>)data);
                mBuyingMembersAdapter.notifyDataSetChanged();
            }
        });
    }

    /** 返回 */
    @OnClick(R.id.LButton)
    public void htImgClick(){

        finish();
    }

    /** 隐私政策 */
    @OnClick(R.id.yszcView)
    public void yszcViewClick(){

        mBuyingMembersPresenter.Agreement(new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                AgreementActivity.startAgreementActivity(BuyingMembersActivity.this,"隐私政策",data + "");
            }
        });
    }

    /** 支付宝支付 */
    private void zfb(final String info){

        new AlipayUtil(BuyingMembersActivity.this, new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (msg.what == 2000) {
                    Map<String, String> result = (Map<String, String>) msg.obj;
                    new ToastUtil(BuyingMembersActivity.this).getToast(false,result.get("memo")).show();
                    setResult(RESULT_OK);

                    Map<String,Object> mmm = (Map<String, Object>) msg.obj;

                    if ((Integer.parseInt(mmm.get("resultStatus") + ""))==9000){

                        BuyingMembersActivity.this.startActivity(new Intent(BuyingMembersActivity.this,WXPayEntryActivity.class));
                        finish();

                    }else {

                        Intent intent = new Intent(BuyingMembersActivity.this,WXPayEntryActivity.class);
                        intent.putExtra("no","no");

                        BuyingMembersActivity.this.startActivity(intent);
                        finish();
                    }

                }

                System.out.println("支付宝:" + msg.toString());
            }
        }).Payment(info, 2000);

    }

    /** 微信支付 */
    private void wx(Map<String, String> Param){

        new WeChatUtil(BuyingMembersActivity.this)
                .Payment(Param);
    }

}
